<!--
* description: 良品登记
* author: xuliwen
* createdDate: 2021-01-21
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :key="oneTableKey"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="true"
      :dynamic-is-init-table="true"
      :dynamic-is-show-more-btn="true"
      :dynamic-api-style="'restful'"
      :callback-obj="callbackObj"
    />
    <edit
      :key="editPopupsKey"
      :dynamic-edit-row-data="editRowData"
      :popups-visible="editPopupsVisible"
      :popups-state="editPopupsState"
      :dynamic-api-style="'restful'"
      @close="close"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import { exportExcel } from '@/utils/getTimeString.js'
import api from '@/api/pa/apiBranchWarehouse'
import request from '@/utils/request'
import Vue from 'vue'

export default {
  name: 'DefectiveProductsRegister',

  components: {
    OneTableTemplate,
    Edit: () => import('./edit')
  },
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ''], function() {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {
      icon: 'el-icon-arrow-down',
      status: false,
      editRowData: {},
      selectiondata: [],
      // 网格查询API配置对象
      apiConfig: api.queryUsable,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          name: 'search',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          position: 'right',
          fuzzySearch: true,
        },
        {
          compKey: 'btnKey2',
          type: '',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          position: 'right' // 重置
        },
        {
          compKey: 'btnKey3',
          type: '',
          size: 'small',
          name: 'add',
          clickEvent: this.add,
          text: this.$t('sys.button.add'), // 新增
          position: 'left'
        },
        {
          compKey: 'btnKey3',
          type: '',
          size: 'small',
          disabled: true,
          parentFileds: 'disabled:table',
          clickEvent: () => this.submit(),
          text: '提交', // 提交
          name: 'adopt',
          position: 'left'
        },
        {
          compKey: 'btnKey3',
          type: '',
          size: 'small',
          disabled: true,
          parentFileds: 'disabled:table',
          clickEvent: () => this.updateRejectsCancel(),
          text: '作废', // 作废
          name: 'adopt',
          position: 'left'
        },
        {
          compKey: 'btnKey4',
          type: '',
          size: 'small',
          clickEvent: () => this.exportExcel111(),
          text: '导出',
          name: 'export',
          position: 'left'
        }
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '仓库名称：',
              codeField: 'warehouseId',
              component: () => import('@/components/org/commonSelect'),
              textField: 'warehouseName',
              customObj: {
                api: api.queryWarehouseNameSelect,
                defOptionFields: ['warehouseId', 'warehouseName']
              },
              type: 'dropdownList',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '备件编码：',
              codeField: 'partNo',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '备件名称：',
              codeField: 'partName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '审核状态：',
              codeField: 'status',
              // lookuptype:'VE0185',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '0', statusText: '未提交' },

                { statusId: '3', statusText: '审核驳回' },

              ],
              type: 'dropdownList',
              isMul: false,
              isMust: true
            }
          ],

      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'partId',
              label: '备件ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'storageId',
              label: 'storageId',
              hidden: true,
              alwaysHidden: true
            },

            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'warehouseId',
              label: '仓库ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'warehouseName',
              label: '仓库名称',
              width: '150px'
            },
            {
              prop: 'partNo',
              label: '备件编号'
            },
            {
              prop: 'partName',
              label: '备件名称'
            },

            {
              prop: 'accountQty',
              label: '账面库存'
            },
            {
              prop: 'badProductsQty',
              label: '不良品'
            },
            {
              prop: 'productsQtyAS',
              label: '良品'
            },
            {
              prop: 'toAbleQtr',
              label: '不良品转良品+',
              width: '100px',
              isComponent: true,
              comps: [
                {
                  compKey: 'compKey5' + this.$utils.generateId(),
                  codeField: 'toAbleQtr',
                  component: () =>
                    import('@/components/org/commonInput/numberInput.vue'),
                  isInput: true,
                  dataToObject: {
                    min: 0,
                    empty: true
                  },
                  decPoint: 0,
                  step: 1
                }
              ]
            },
            {
              prop: 'toUnableQty',
              label: '良品转不良品+',
              width: '100px',
              isComponent: true,
              comps: [
                {
                  compKey: 'compKey5' + this.$utils.generateId(),
                  codeField: 'toUnableQty',
                  component: () =>
                    import('@/components/org/commonInput/numberInput.vue'),
                  isInput: true,
                  dataToObject: {
                    min: 0,
                    empty: true
                  },
                  decPoint: 0,
                  step: 1
                }
              ]
            },
            {
              prop: 'remark',
              label: '备注+',
              isComponent: true,
              comps: [
                {
                  compKey: 'compKey5' + this.$utils.generateId(),
                  codeField: 'remark',
                  component: () => import('@/components/org/commonInput'),
                  // isInput: true,
                  // dataToObject: {
                  //   empty: true,
                  // },
                  type: 'inputText'
                }
              ]
            },
            {
              prop: 'applyer',
              label: '登记人+',
              isComponent: true,
              comps: [
                {
                  compKey: 'compKey5' + this.$utils.generateId(),
                  codeField: 'applyer',
                  component: () => import('@/components/org/commonInput'),
                  isInput: true,
                  dataToObject: {
                    empty: true
                  },
                  type: 'inputText'
                }
              ]
            },
            {
              prop: 'applyDate',
              label: '登记时间',
              width: '150px'
            },
            {
              prop: 'status',
              label: '状态'
            },
            {
              prop: 'auditMark',
              label: '驳回原因'
            }
          ],
      callbackObj: {
        // 手动勾选时
        onCheckSelect: (selection, row) => {
          this.selectiondata = selection
        },
        onCheckSelectChange: (selection) => {
          this.onCheckSelectChange1(selection)
        }
      },
      // 表单查询数据
      formField: this.$utils.getFormField(
        this,
        {
          warehouseId: '',
          partName: '',
          // status: '',
          partNo: '',
          status:["0","3"]
          // status:"1"
        },
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode]
      )
    }
  },
  mounted() {
    // 列表查询
    this.queryTable(1)
  },

  methods: {
    onCheckSelectChange1(selection) {
      var btn1 = this.tableButtons[3]
      var btn2 = this.tableButtons[4]
      const isCheck = selection && selection.length > 0
      btn1.disabled = !isCheck
      btn2.disabled = !isCheck
    },
    // 导出
    exportExcel111() {
      // 导出
      exportExcel(
        this.formField,
        api.exportUsable.APIUrl,
        api.exportUsable.ServiceCode,
        '网点不良品登记'
      )
    },
    // 提交
    submit() {
      request({
        url: api.updateRejectsSubmit.APIUrl,
        method: 'post',
        data: this.selectiondata
      }).then((res) => {
        this.$message.success(res.msg)
        this.queryTable(1)
      })
    },
    // 作废
    updateRejectsCancel() {
      request({
        url: api.updateRejectsCancel.APIUrl,
        method: 'post',
        data: this.selectiondata
      }).then((res) => {
        this.$message.success(res.msg)
        this.queryTable(1)
      })
    }
  }
}
</script>
<style lang='scss' scoped>
.red {
  background: red;
}
</style>
